﻿using ShiQuan.SmartDevice;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ShiQuan.HardDisk
{
    public class FrmMain : Form
    {
        private ToolStrip toolStrip1;
        private ToolStripButton tbbRead;
        private ToolStripDropDownButton tbbHelp;
        private ToolStripMenuItem mnuAbout;
        private StatusStrip sysStatusBar;
        private ToolStripStatusLabel sysStatusLabel;
        private RichTextBox richTextBox1;
        private ToolStripStatusLabel sysStatusDate;
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源，为 true；否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FrmMain));
            this.toolStrip1 = new System.Windows.Forms.ToolStrip();
            this.tbbRead = new System.Windows.Forms.ToolStripButton();
            this.tbbHelp = new System.Windows.Forms.ToolStripDropDownButton();
            this.mnuAbout = new System.Windows.Forms.ToolStripMenuItem();
            this.sysStatusBar = new System.Windows.Forms.StatusStrip();
            this.sysStatusLabel = new System.Windows.Forms.ToolStripStatusLabel();
            this.sysStatusDate = new System.Windows.Forms.ToolStripStatusLabel();
            this.richTextBox1 = new System.Windows.Forms.RichTextBox();
            this.toolStrip1.SuspendLayout();
            this.sysStatusBar.SuspendLayout();
            this.SuspendLayout();
            // 
            // toolStrip1
            // 
            this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.tbbRead,
            this.tbbHelp});
            this.toolStrip1.Location = new System.Drawing.Point(0, 0);
            this.toolStrip1.Name = "toolStrip1";
            this.toolStrip1.Size = new System.Drawing.Size(384, 25);
            this.toolStrip1.TabIndex = 4;
            this.toolStrip1.Text = "toolStrip1";
            // 
            // tbbRead
            // 
            this.tbbRead.Image = ((System.Drawing.Image)(resources.GetObject("tbbRead.Image")));
            this.tbbRead.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.tbbRead.Name = "tbbRead";
            this.tbbRead.Size = new System.Drawing.Size(76, 22);
            this.tbbRead.Text = "开始读取";
            // 
            // tbbHelp
            // 
            this.tbbHelp.Alignment = System.Windows.Forms.ToolStripItemAlignment.Right;
            this.tbbHelp.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.mnuAbout});
            this.tbbHelp.Image = ((System.Drawing.Image)(resources.GetObject("tbbHelp.Image")));
            this.tbbHelp.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.tbbHelp.Name = "tbbHelp";
            this.tbbHelp.Size = new System.Drawing.Size(61, 22);
            this.tbbHelp.Text = "帮助";
            // 
            // mnuAbout
            // 
            this.mnuAbout.Name = "mnuAbout";
            this.mnuAbout.Size = new System.Drawing.Size(152, 22);
            this.mnuAbout.Text = "关于";
            // 
            // sysStatusBar
            // 
            this.sysStatusBar.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.sysStatusLabel,
            this.sysStatusDate});
            this.sysStatusBar.Location = new System.Drawing.Point(0, 335);
            this.sysStatusBar.Name = "sysStatusBar";
            this.sysStatusBar.Size = new System.Drawing.Size(384, 26);
            this.sysStatusBar.SizingGrip = false;
            this.sysStatusBar.TabIndex = 6;
            // 
            // sysStatusLabel
            // 
            this.sysStatusLabel.BorderSides = ((System.Windows.Forms.ToolStripStatusLabelBorderSides)((System.Windows.Forms.ToolStripStatusLabelBorderSides.Left | System.Windows.Forms.ToolStripStatusLabelBorderSides.Right)));
            this.sysStatusLabel.Name = "sysStatusLabel";
            this.sysStatusLabel.Size = new System.Drawing.Size(287, 21);
            this.sysStatusLabel.Spring = true;
            this.sysStatusLabel.Text = "技术支持：896374871@qq.com";
            this.sysStatusLabel.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // sysStatusDate
            // 
            this.sysStatusDate.AutoSize = false;
            this.sysStatusDate.BorderSides = ((System.Windows.Forms.ToolStripStatusLabelBorderSides)((System.Windows.Forms.ToolStripStatusLabelBorderSides.Left | System.Windows.Forms.ToolStripStatusLabelBorderSides.Right)));
            this.sysStatusDate.Name = "sysStatusDate";
            this.sysStatusDate.Size = new System.Drawing.Size(100, 21);
            this.sysStatusDate.Text = "2020-05-05";
            this.sysStatusDate.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // richTextBox1
            // 
            this.richTextBox1.BackColor = System.Drawing.SystemColors.Info;
            this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.richTextBox1.Location = new System.Drawing.Point(0, 25);
            this.richTextBox1.Name = "richTextBox1";
            this.richTextBox1.ReadOnly = true;
            this.richTextBox1.Size = new System.Drawing.Size(384, 310);
            this.richTextBox1.TabIndex = 7;
            this.richTextBox1.Text = "";
            // 
            // FrmMain
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(384, 361);
            this.Controls.Add(this.richTextBox1);
            this.Controls.Add(this.sysStatusBar);
            this.Controls.Add(this.toolStrip1);
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "FrmMain";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Form1";
            this.toolStrip1.ResumeLayout(false);
            this.toolStrip1.PerformLayout();
            this.sysStatusBar.ResumeLayout(false);
            this.sysStatusBar.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        public FrmMain()
        {
            InitializeComponent();

            this.Load += FrmMain_Load;
        }

        void FrmMain_Load(object sender, EventArgs e)
        {
            this.Text = Application.ProductName + "("+Application.ProductVersion+")";
            this.sysStatusDate.Text = DateTime.Today.ToString("yyyy-MM-dd");

            this.tbbRead.Click += tbbRead_Click;
            this.mnuAbout.Click += mnuAbout_Click;
        }

        void mnuAbout_Click(object sender, EventArgs e)
        {
            FrmAbout frm = new FrmAbout();
            frm.Show();
        }

        void tbbRead_Click(object sender, EventArgs e)
        {
            this.tbbRead.Enabled = false;
            this.Cursor = Cursors.WaitCursor;
            StringBuilder content = new StringBuilder();
            try
            {
                var hardDisk = ShiQuan.SmartDevice.AtapiDevice.GetHddInfo(0);  // 第一个硬盘
                content.AppendLine("atapi");
                content.AppendLine("Module Number=" + hardDisk.ModuleNumber);
                content.AppendLine("SerialNumber=" + hardDisk.SerialNumber);
                content.AppendLine("Firmware=" + hardDisk.Firmware);
                content.AppendLine("Capacity=" + hardDisk.Capacity);
            }
            catch (Exception ex)
            {
                content.AppendLine("Atapi读取异常：" + ex.Message);
            }
            content.AppendLine("====\nWin32 Management 读取...");
            try
            {
                ShiQuan.SmartDevice.Win32MachineHelper win32Helper = new SmartDevice.Win32MachineHelper();

                content.AppendLine("Win32_PhysicalMedia:" + win32Helper.GetSerialNumber());

                content.AppendLine("Win32_DiskDrive:" + win32Helper.GetDiskDriveId());

                content.AppendLine("MacAddress:" + win32Helper.GetMacAddress());

                //content.AppendLine("Win32_Processor:" + win32Helper.GetProcessorId());
            }
            catch (Exception ex)
            {
                content.AppendLine("Win32读取异常：" + ex.Message);
            }
            //content.AppendLine("====\nKernel32 读取...");
            //try
            //{
            //    VolumeInfo volumeInfo = WinKernel32Helper.GetVolumeInfo();

            //    content.AppendLine("Name:" + volumeInfo.Name);

            //    content.AppendLine("SerialNumber:" + volumeInfo.SerialNumber);

            //    content.AppendLine("FileSystem:" + volumeInfo.FileSystem);
            //}
            //catch (Exception ex)
            //{
            //    content.AppendLine("Win32读取异常：" + ex.Message);
            //}
            this.tbbRead.Enabled = true;
            this.Cursor = Cursors.Default;
            this.richTextBox1.Text = content.ToString();
        }

        void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
